package com.jzr.medical.common;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class MyServiceImpl<VM extends BaseMapper<V>, V extends T,TM extends BaseMapper<T>,  T extends IdEntity> extends ServiceImpl<VM,V> {
    @Autowired
    protected TM entityMapper;

    @Override
    public boolean save(V record){
        return this.retBool(entityMapper.insert(record));
    }

    @Override
    public boolean updateById(V record){
        if(record.getId() == null){
            return this.save(record);
        }
        return this.retBool(entityMapper.updateById(record));
    }

    @Override
    public boolean removeById(Serializable id){
        return this.retBool(entityMapper.deleteById(id));
    }

    @Override
    public boolean removeByMap(Map<String, Object> columnMap){
        return this.retBool(entityMapper.deleteByMap(columnMap));
    }

    public boolean delete(Wrapper<T> wrapper){
        return this.retBool(entityMapper.delete(wrapper));
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList){
        return this.retBool(entityMapper.deleteBatchIds(idList));
    }

    public boolean update(T entity, Wrapper<T> updateWrapper){
        return this.retBool(entityMapper.update(entity,updateWrapper));
    }

    public Integer getId(QueryWrapper<V> qw) {
        qw.select("id");
        List<Object> list = baseMapper.selectObjs(qw);
        return list == null || list.size()<=0 ? null : (Integer)list.get(0);
    }

}
